﻿using System;
using Sunny.UI;
using System.Windows.Forms;
using CoreShop.Model;
using CoreShop.Library;
using CoreShop.Services;
using CoreShop.App.Units.Interface;
using Loading;
using System.Threading.Tasks;

namespace CoreShop.App
{
    public partial class frmModifyPassword : UIForm
    {
        OpaqueCommand cmd = new OpaqueCommand();
        public corecmsuser user;
        public frmModifyPassword(corecmsuser _user)
        {
            user = _user;
            InitializeComponent();
            uiTextBox1.TextBox.GotFocus += txtSendKey_GotFocus;
            //this.pnlTitle.FillColor = SettingModelUtil.settingsModel.dIALOG.MAINCOLOR;
            //this.pnlTitle.FillColor2 = SettingModelUtil.settingsModel.dIALOG.MAINCOLOR;
        }

        private void txtSendKey_GotFocus(object sender, System.EventArgs e)
        {
            numPad.SetTextBox((UIEdit)sender, UITextBox.UIEditType.Integer, 0);
        }

        private async void uiButton3_ClickAsync(object sender, EventArgs e)
        {
            cmd.ShowOpaqueLayer(pnl_Form, 125, true);
            try
            {
                ReturnModel returnModel = await MemberApiUtil.SendSMSCode(AppConstVars.sysUser, user);

                if (returnModel.Status != ReturnStatus.Success)
                {
                    UIMessageBox.Show($"发送验证码失败,{returnModel.MessageOrString}");
                    return;
                }

                ApiReturnModel apiReturnModel = returnModel.MessageOrString.JsonToEntity<ApiReturnModel>();

                if (apiReturnModel.code != 200)
                {
                    //$"发送验证码失败,{apiReturnModel.msg}".ShowError();
                    //LogManage.Error($"发送验证码失败,{apiReturnModel.msg}");
                    return;
                }

                uiButton3.Invoke(new Action(() =>
                {
                    uiButton3.Text = "60";
                    uiButton3.Enabled = false;
                }));

                uiMillisecondTimer1.Start();
                UIMessageBox.Show("发送成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cmd.HideOpaqueLayer();
            }
        }

        private async void SendOk()
        {
            cmd.ShowOpaqueLayer(pnl_Form, 125, true);
            try
            {
                ReturnModel returnModel = await MemberApiUtil.UpdatePassword(AppConstVars.sysUser, user, uiTextBox1.Text.Trim(), uiTextBox2.Text.Trim(), uiTextBox4.Text.Trim());

                if (returnModel.Status != ReturnStatus.Success)
                {
                    UIMessageBox.Show($"改密失败,{returnModel.MessageOrString}");
                    return;
                }

                ApiReturnModel apiReturnModel = returnModel.MessageOrString.JsonToEntity<ApiReturnModel>();

                if (apiReturnModel.code != 200)
                {
                    UIMessageBox.Show($"改密失败,{apiReturnModel.msg}");
                    return;
                }

                UIMessageBox.Show("改密成功");

                this.Invoke(new Action(() =>
                {
                    DialogResult = DialogResult.Yes;
                }));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cmd.HideOpaqueLayer();
            }
        }

        private void uiMillisecondTimer1_Tick(object sender, EventArgs e)
        {
            this.Invoke(new Action(() =>
            {
                int tm = uiButton3.Text.ToInt();

                if (--tm > 1)
                {
                    uiButton3.Text = tm.ToString();
                }
                else
                {
                    uiButton3.Text = "获取";
                    uiButton3.Enabled = true;
                    uiMillisecondTimer1.Stop();
                }
            }));
        }

        private void uiTextBox1_TextChanged(object sender, EventArgs e)
        {
            ChanckPassword();
        }

        /// <summary>
        /// 检查密码
        /// </summary>
        void ChanckPassword()
        {
            uiLabel3.Text =
                uiTextBox1.Text == uiTextBox2.Text ? string.Empty : "两次输入的密码不一致";
        }

        private void uiTextBox2_TextChanged(object sender, EventArgs e)
        {
            ChanckPassword();
        }

        private void GsMemberUpdatePassword17_Shown(object sender, EventArgs e)
        {
            uiTextBox1.Focus();
        }

        private void lblClose_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void uiNumPad1_OKButtonClick(object sender, string text, int intvalue, double doublevalue)
        {
            SendOk();
        }
    }
}
